<?php
  error_reporting(E_ALL ^ E_DEPRECATED);

	$brands = array(
	1=>'Advance',
	2=>'Airwave',
	3=>'Gin',
	4=>'Icaro',
	5=>'Nova',
	6=>'Skywalk',
	7=>'Swing',
	8=>'UP',
	9=>'SOL',
	10=>'Gradient',
	11=>'FreeX',
	12=>'ProDesign',
	13=>'Mac Para',
	14=>'UTurn',
	15=>'Apco',
	16=>'Woc',
	17=>'Windtech',
	18=>'Sky',
	19=>'Ozone',
	20=>'Aerodyne',
	21=>'Aircross',
	22=>'Paratech',
	23=>'Dudek',
	24=>'Firebird',
	25=>'FlightDesign',
	26=>'Niviuk',
	27=>'Independence',
	28=>'Axis',
	29=>'Aeros',
	30=>'Edel',
	31=>'ITV',
	32=>'Nervures',
	33=>'Pegas',
	34=>'Perche',
	35=>'Trekking',
	36=>'XiX',
	37=>'Paradelta',
	38=>'WillsWing',
	39=>'Moyes',
	40=>'AIR',
	41=>'Airborne',
	42=>'Bautek',
	43=>'Seedwings',
	44=>'Finsterwalder',
	45=>'Team5',
	46=>'Thalhofer',
	47=>'Airea',
	48=>'La Mouette',
	49=>'Impuls',
	50=>'Team Impact',
	51=>'Guggenmos',
	52=>'FlyingPlanet',
	53=>'EnterpriseWings',
	54=>'CaliptAir',
	55=>'Tecma',
	56=>'Icaro 2000',
	57=>'Skyline',
	58=>'Skyward',
	59=>'Escape',
	60=>'Mcc',
	61=>'Skyjam',
	62=>'Airsport',
	63=>'Sun',
	65=>'ZeroG',
	66=>'Artwing',
	67=>'Pilots',
	68=>'Ol',
	69=>'Bilsam',
	70=>'CpProducts',
	71=>'Sfair',
	72=>'CustomSail',
	73=>'Freestyle',
	74=>'Kimfly',
	75=>'ASA',
	76=>'ParaAvis',
	77=>'Jojowing',
	78=>'Pilot',
	79=>'Stiebritz',
	80=>'AirSystems',
	81=>'JLD',
	82=>'Skif',
	83=>'Galaxy',
	84=>'E2RA',
	85=>'Element',
	86=>'Skycountry',
  87=>'BioAir',
	);

  function getId(&$brands, $brand) {
    $brand = strtolower($brand);

    foreach ($brands as $key => $value) {
      if (strtolower($value) == $brand)
        return $key;
    }
  }

  function getUserInput() {
    $handle = fopen ("php://stdin","r");
    $line = fgets($handle);

    /*if(trim($line) != 'yes'){
      echo "ABORTING!\n";
      exit;
    }*/
    return $line;
  }

  mysql_connect('localhost', 'root', '');
  @mysql_select_db('phpbb') or die( "Unable to select database");

  $myFile = $argv[1];
  $fh = fopen($myFile, 'r');
  while (($theData = fgets($fh))) {
    //echo 'read='.$theData;

    if (trim($theData) != '') {
      $data = split('-', $theData);

      $id = getId($brands, trim($data[0]));
      echo "id for ".$data[0]."=$id, glider: ".trim($data[1])."\n";

      if ($id == '') {
        $id = trim(getUserInput());
      }

      $query = "insert into portal_glider(brand,model,cert_en,cert_ltf,created_at, updated_at) values(
        '$id','".ucwords(strtolower(trim($data[1])))."','D','2-3','2010-05-20 13:18:24','2010-05-20 13:18:24')";

      echo "query=$query\n";
      mysql_query($query);
      echo "inserted\n\n";
    }
  };
  fclose($fh);

  mysql_close();

?>
